package com.supermate.supercut.app.service;

import com.supermate.init.util.DateUtil;
import com.supermate.supercut.app.constant.AppConstant;
import com.supermate.supercut.app.enums.ErrorEnums;
import com.supermate.supercut.app.enums.ExceptionsGetter;
import com.supermate.supercut.app.vo.FileVo;
import com.supermate.supercut.app.vo.ResourceVo;
import com.supermate.supercut.domain.model.ClipboardText;
import com.supermate.supercut.domain.model.FileResource;
import com.supermate.supercut.domain.repo.ClipboardResourceRepo;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: 冰岩
 * @Date: 2023/10/28
 * @Description:
 */
@Component
public class ClipboardReadService {

    @Resource
    private ClipboardResourceRepo clipboardResourceRepo;

    public ResourceVo getClipboardResources(String clipboardName) {
        ClipboardText clipboardText = clipboardResourceRepo.queryClipboardText(clipboardName);
        List<FileResource> fileResources = clipboardResourceRepo.queryFileLists(clipboardName);
        return toResourceVo(clipboardText, fileResources);
    }

    public FileResource getFileResourceByClipNameAndId(String clipboardName, String fileId) {
        List<FileResource> fileResources = clipboardResourceRepo.queryFileLists(clipboardName);
        FileResource fileResource = Optional.ofNullable(fileResources).orElse(Collections.emptyList())
            .stream().filter(x -> Objects.equals(String.valueOf(x.getId()), fileId))
            .findFirst().orElse(null);
        if (Objects.isNull(fileResource)) {
            throw ExceptionsGetter.getBizException(ErrorEnums.CLIPBOARD_NOT_EXIST_THE_FILE);
        }
        return fileResource;
    }


    private ResourceVo toResourceVo(ClipboardText clipboardText, List<FileResource> fileResources) {
        ResourceVo resourceVo = new ResourceVo();
        resourceVo.setFileList(new ArrayList<>());
        if (Objects.nonNull(clipboardText)) {
            resourceVo.setTextId(clipboardText.getId());
            resourceVo.setClipboardName(clipboardText.getClipboardName());
            resourceVo.setClipboardText(clipboardText.getClipboardText());
            resourceVo.setGmtCreate(DateUtil.date2StrYMDHMS(clipboardText.getGmtCreate()).orElse(null));
            resourceVo.setGmtModify(DateUtil.date2StrYMDHMS(clipboardText.getGmtModify()).orElse(null));
            resourceVo.setViewTimes(clipboardText.getViewTimes());
        }
        if (Objects.isNull(fileResources)) {
            return resourceVo;
        }
        fileResources.forEach(fileResource -> {
            resourceVo.getFileList().add(toFileVo(fileResource));
        });
        return resourceVo;
    }

    private FileVo toFileVo(FileResource fileResource) {
        FileVo fileVo = new FileVo();
        fileVo.setFileId(fileResource.getId());
        fileVo.setClipboardName(fileResource.getClipboardName());
        fileVo.setFileName(fileResource.getFileName());
        fileVo.setDownLoadTimes(fileResource.getDownLoadTimes());
        fileVo.setFileDownLoadLink(AppConstant.getFileHttpDownloadLink(fileResource.getRelativePath()));
        fileVo.setPreviewImage(fileResource.getPreviewImgs());
        fileVo.setUploadTime(DateUtil.date2StrYMDHMS(fileResource.getUploadTime()).orElse(null));
        fileVo.setGmtModify(DateUtil.date2StrYMDHMS(fileResource.getGmtModify()).orElse(null));
        return fileVo;
    }

}
